module.exports = {
    create: (req, res, next) => {
      const requiredFields = ['author_id', 'name', 'image_url', 'description', 'background', 'preferences'];
      const missing = requiredFields.filter(field => !req.body[field]);
      
      if (missing.length > 0) {
        return res.status(400).json({
          error: `Missing required fields: ${missing.join(', ')}`
        });
      }
  
      try {
        JSON.parse(req.body.preferences);
        if (req.body.extra_info) {
          JSON.parse(req.body.extra_info);
        }
        next();
      } catch (e) {
        res.status(400).json({ error: 'Invalid JSON format' });
      }
    },
  
    update: (req, res, next) => {
      const allowedFields = ['name', 'image_url', 'description', 'background', 'preferences', 'extra_info'];
      const invalid = Object.keys(req.body).filter(field => !allowedFields.includes(field));
      
      if (invalid.length > 0) {
        return res.status(400).json({
          error: `Invalid fields: ${invalid.join(', ')}`
        });
      }
      
      if (req.body.preferences) {
        try {
          JSON.parse(req.body.preferences);
        } catch (e) {
          return res.status(400).json({ error: 'Invalid preferences format' });
        }
      }
      
      next();
    }
  };